﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using System.Xml;
using VitCode.Castle.DataAccess;

namespace AMSReportBuildService
{
    public partial class AMSReportBuildService : ServiceBase
    {
        string configFile = System.Windows.Forms.Application.StartupPath + @"\ServiceConfig.xml";
        public AMSReportBuildService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            this.timer1.Start();
            this.timer1.Enabled = true;

        }





        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            this.timer1.Stop();
            this.timer1.Enabled = false;
        }


        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                var m_XmlReader = new ObjXmlReader(configFile);
                XmlNode node = m_XmlReader.GetXmlNode("serviceConfig/filePath");
                if (node != null)
                {
                    string path = m_XmlReader.GetNodeAttributeValue(node, "value", true);


                    if (DateTime.Now.Hour == 13)
                    {
                        var ad = new AssetDistributeXmlFile();
                        ad.CreateXmlFile(path + "DistributeOfAsset.xml");
                        var avd = new AssetValueDistributeXmlFile();
                        avd.CreateXmlFile(path + "DistributeOfAssetValue.xml");
                        var adv = new AssetDistributeByAreaXmlFile();
                        adv.CreateXmlFile(path + "DistributeByAreaOfAsset.xml");
                        var advv = new AssetValueDistributeByAreaXmlFile();
                        advv.CreateXmlFile(path + "DistributeByAreaOfAssetValue.xml");
                        var aif = new AssetIncrementXmlFile();
                        aif.CreateAssetGraphXmlFile(path + "GrowthTrendOfAsset.xml");
                        aif.CreateAssetListXmlFile(path + "GrowthTrendOfAssettb.xml");
                        var alf = new AssetLessenXmlFile();
                        alf.CreateAssetGraphXmlFile(path + "LessenTrendOfAsset.xml");
                        alf.CreateAssetListXmlFile(path + "LessenTrendOfAssettb.xml");
                        var acf = new AssetChangeXmlFile();
                        acf.CreateAssetGraphXmlFile(path + "ChangeOfAsset.xml");
                        acf.CreateAssetListXmlFile(path + "ChangeOfAssettb.xml");
                      
                        
                    }
                }
            }
            catch (Exception error)
            {
                //写错误日志
                using (var sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + @"" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true, System.Text.Encoding.UTF8))
                {
                    sw.WriteLine(DateTime.Now.ToString() + "：");
                    sw.WriteLine(error.ToString());
                    sw.WriteLine("---------------------------------------------");
                    sw.Close();
                }
                throw;
            }
           
        }
    }
}
